home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / examples / am next >
Encoding:
Text File  |  1994-02-08  |  6.4 KB  |  251 lines

  1. #! /usr/local/bin/icmake -qi
  2.  
  3. /*
  4.                             AM
  5.  
  6.        This script is used to implement a non-destructive rm
  7. */
  8.  
  9. #define YEAR    "1994"
  10. #define VERSION "1.11"
  11.  
  12. int
  13.     flags_done,
  14.     extract,
  15.     viewmode,
  16.     debug;
  17.  
  18. string
  19.     home,
  20.     attic,
  21.     cwd,
  22.     progname,
  23.     recurs,
  24.     forced,
  25.     unzipflag;
  26.  
  27. void kill(string s)
  28. {
  29.     printf(s, "\n\n");
  30.     exit(1);
  31. }
  32.  
  33. void preamble(list argv, list envp)
  34. {
  35.     int
  36.         index;
  37.  
  38.     cwd = chdir(".");                       // get cwd
  39.  
  40.     for (index = 0; home = element(index, envp); index += 2)
  41.     {
  42.         if (home == "HOME")                 // HOME found
  43.         {                                   // get it
  44.             home = element(index + 1, envp);
  45.             break;                          // and done
  46.         }
  47.     }
  48.     if (!home)
  49.         kill("$HOME not found");
  50.  
  51.     progname = change_ext(element(0, argv), "");
  52.  
  53.     attic = home + "/attic";                // set $HOME/attic, change to
  54. }
  55.  
  56. void check_attic()
  57. {
  58.     if (!exists(attic))                     // attic should exist
  59.     {
  60.         printf(attic, " does not exist. Create it [y/n] ? ");
  61.         if (getch() != "y")                 // not a "y" ?
  62.             kill("ok.");
  63.         system("mkdir " + attic);           // make the attic subdir
  64.         exec("chmod", 700, attic);          // private use
  65.     }                                       // else attic must be dir
  66.     else if (!((int)element(0, stat(attic)) & S_IFDIR))
  67.         kill("'" + attic + "' is not a directory");
  68.  
  69.     attic += "/attic";                      // append the zip-name
  70.     chdir("/");                             // go to the root
  71. }
  72.  
  73. void set_flags(string arg)
  74. {
  75.     int
  76.         index;
  77.     string
  78.         flag;
  79.  
  80.                                             // process all arguments
  81.     for (index = 1; flag = element(index, arg); index++)
  82.     {
  83.         if (flag == "r")                    // process encountered options
  84.             recurs = "-r";
  85.         else if (flag == "d")
  86.             debug++;
  87.         else if (flag == "f")
  88.             forced = "-f";
  89.         else if (flag == "x")
  90.             extract++;
  91.         else if (flag == "v")
  92.         {
  93.             extract++;
  94.             viewmode++;
  95.             unzipflag = "-l ";
  96.         }
  97.         else
  98.             kill("Unrecognized flag '-" +
  99.                  flag +
  100.                  "': " +
  101.                  progname +
  102.                  "aborted");
  103.     }
  104.  
  105.     if (extract && unzipflag == "")
  106.         unzipflag = "-u ";                  // use proper unzip flag
  107. }
  108.  
  109. list options(int argc, list argv)
  110. {
  111.     int
  112.         index;
  113.     list
  114.         ret;
  115.     string
  116.         arg;
  117.  
  118.     for (index = 0; index < argc; index++)
  119.     {
  120.         arg = element(index, argv);         // get next argument
  121.         if (element(0, arg) == "-")         // first element is a - ?
  122.             set_flags(arg);                 // then set flags
  123.         else
  124.             ret += (list)arg;               // or add to list to return
  125.     }
  126.  
  127.     return (ret);                           // returned list
  128. }
  129.  
  130. void usage()
  131. {
  132.     printf
  133.     (
  134.         "\n"
  135.         "ICCE AM (Attic Move) non-destructive remove. Version " VERSION "\n"
  136.         "Copyright (c) ICCE " YEAR ". All Rights Reserved\n"
  137.         "\n"
  138.         "AM by Frank B. Brokken\n"
  139.         "\n"
  140.         "Usage: ", progname, " [options] file(s)\n"
  141.         "Where:\n"
  142.         "   options:\n"
  143.         "       -d: Debug mode: no execution but display of commands\n"
  144.         "       -f: Forced processing of indicated files\n"
  145.         "       -r: Recursive removal of directory contents\n"
  146.         "       -v: View current contents of the attic\n"
  147.         "       -x: Extract files from the attic to their original place\n"
  148.         "           (i.e., if you are permitted to do so...\n"
  149.         "\n"
  150.         "   file(s): names of files and directories to move to/from the attic\n"
  151.         "\n"
  152.         "   ", home, "/attic/attic.zip is used to store the files.\n"
  153.         "\n"
  154.     );
  155.     exit (1);
  156. }
  157.  
  158. string prefix_path(string file)
  159. {
  160.     string
  161.         el,
  162.         ret;
  163.     int
  164.         index;
  165.  
  166.     if (element(0, file) != "/")            // if file isn't an absolute path
  167.         file = cwd + file;                  // then make an absolute path
  168.  
  169.     for (index = 1; el = element(index, file); index++)
  170.         ret += el;                          // remove first char from abs path
  171.  
  172.     return (ret);                           // return modified string
  173. }
  174.  
  175. void retrieve(string file)
  176. {
  177.     string
  178.         cmd;
  179.  
  180.     cmd = "unzip "                          // update only
  181.         + unzipflag
  182.         + attic;
  183.  
  184.     if (!viewmode)
  185.         cmd += " "
  186.              + prefix_path(file);           // and the file (+ path prefix)
  187.  
  188.     if (debug)
  189.         printf("( cd /; ", cmd, " )\n");    // debug: show command
  190.     else
  191.         system(cmd);                        // else exec cmd
  192. }
  193.  
  194. void remove(string file)
  195. {
  196.     string
  197.         cmd;
  198.  
  199.     cmd = "zip -my "                        // remove, remove links as links
  200.         + forced                            // maybe forced
  201.         + " "
  202.         + recurs                            // maybe recursive
  203.         + " "
  204.         + attic                             // target zip
  205.         + " "
  206.         + prefix_path(file);                // and the file (+ path prefix)
  207.  
  208.     if (debug)
  209.         printf("( cd /; ", cmd, " )\n");    // debug: show command
  210.     else
  211.         system(cmd);                        // else exec cmd
  212. }
  213.  
  214. void one_file(string file)
  215. {
  216.     if (extract)                            // either retrieve or remove
  217.         retrieve(file);                     // the file
  218.     else
  219.         remove(file);
  220. }
  221.  
  222. void process(int argc, list argv)
  223. {
  224.     int
  225.         index;
  226.  
  227.     for (index = 1; index < argc; index++)
  228.         one_file(element(index, argv));     // process one file
  229. }
  230.  
  231. int main(int argc, list argv, list envp)
  232. {
  233.     echo (OFF);
  234.  
  235.     preamble(argv, envp);                   // set progname and attic dir.
  236.  
  237.     argv = options(argc, argv);             // get the options
  238.     argc = sizeof(argv);                    // determine remaining arguments
  239.  
  240.     if (argc == 1 && !viewmode)             // none left and no viewmode ?
  241.         usage();                            // give usage and exit 1
  242.  
  243.     check_attic();                          // check accessability of attic
  244.  
  245.     if (viewmode)                           // view contents
  246.         retrieve("");
  247.     else                                    // or
  248.         process(argc, argv);                // process remaining arguments
  249.     return (0);                             // done
  250. }
  251.